package com.yiwei.zxp.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.text.Spanned;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yiwei.zxp.R;
import com.yiwei.zxp.activity.GoodDetailsActivity;
import com.yiwei.zxp.activity.GoodDetailsV2Activity;
import com.yiwei.zxp.api.model.BoothGoodsListModel;
import com.yiwei.zxp.util.Spans;
import com.viewpagerindicator.LinePageIndicator;
import com.yiwei.zxp.util.Util;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by xsooy-pc on 2017/10/23.
 */

public class PacketRightAdapter extends ItemAdapter<BoothGoodsListModel.DatasBean, PacketRightAdapter.DefaultHolder> {

    private final int x24;
    private final int x36;

    private Context context;

    public PacketRightAdapter(Context context) {
        super(context);
        x24 = context.getResources().getDimensionPixelSize(R.dimen.x24);
        x36 = context.getResources().getDimensionPixelSize(R.dimen.x36);
        this.context = context;
    }

    @Override
    public DefaultHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DefaultHolder(inflate(R.layout.item_packet_right, parent));
    }

    @Override
    public void onBindViewHolder(DefaultHolder holder, int position) {
        BoothGoodsListModel.DatasBean item = getItem(position);
        Spanned txtMoney = Spans.with(context).font("￥", x24, 0xffFEFEFE).style(Typeface.BOLD).
                font(item.price, x36, 0xffFEFEFE).style(Typeface.BOLD).
                font("\n" + item.title, x24, 0xffffffff).
                build();
        holder.txtInfo.setText(txtMoney);
//        holder.imgIcon.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//
//            }
//        });
        initData(item, holder.imgIcon, item.photos);
    }

    private void initData(BoothGoodsListModel.DatasBean item, ViewPager pager, List<String> photo) {

        //保存图片资源的集合
        pager.removeAllViews();

//        if (photo == null || photo.size() == 0) {
//            indicator.setVisibility(View.GONE);
//            return;
//        }
//        indicator.setVisibility(View.VISIBLE);
        List<ImageView> imageViews = new ArrayList<>();
        ImageView imageView;
//        //循环遍历图片资源，然后保存到集合中
        for (int i = 0; i < photo.size(); i++) {
            //添加图片到集合中
            imageView = new ImageView(context);
            imageView.setLayoutParams(new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
//            imageView.setBackgroundResource(imageResIds[i]);
            imageViews.add(imageView);
        }

        pager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return photo.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                String url = photo.get(position);

                Glide.with(context)
                        .load(url)
                        .placeholder(R.color.image_default)
                        .into(imageViews.get(position));
                imageViews.get(position).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(context, GoodDetailsV2Activity.class);
                        intent.putExtra("goods_id", item.id);
                        intent.putExtra("goods_no", item.goods_no);
                        context.startActivity(intent);
                    }
                });
                container.addView(imageViews.get(position));
                return imageViews.get(position);
            }

            @Override
            public void destroyItem(ViewGroup view, int position, Object object) {
                view.removeView(imageViews.get(position));
            }
        });
//        indicator.setViewPager(pager);
    }

    class DefaultHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img_icon)
        ViewPager imgIcon;
//        @BindView(R.id.indicator)
//        LinePageIndicator indicator;
        @BindView(R.id.txt_info)
        TextView txtInfo;

        public DefaultHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
